package com.example.shop_addres.controller;

import com.example.shop_addres.entity.ShopAddress;
import com.example.shop_addres.service.ShopService;
import com.example.shop_addres.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("shop/shopAddress")
public class ShopController {
    @Autowired
    private ShopService shopService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private ShopAddress shopAddress;
    @PostMapping("register")
    public JsonResult updateAddress(
                                    @RequestParam("mobile") String mobile,
                                    @RequestParam("code") String code,
                                    @RequestParam("name") String name,
                                    @RequestParam("address") String address
    ){
        shopAddress.setName(name);
        shopAddress.setMobile(mobile);
        shopAddress.setAddress(address);
        System.out.println(shopAddress);
        int result = shopService.insertShopAddress(shopAddress);
        if (result == 0) {
            return JsonResult.failed("注册失败");
        }
        return JsonResult.success("创建成功",null);
    }


    // 修改
    @PostMapping("update")
    public JsonResult updateAddress(@RequestParam("mobile") String mobile,
                                    @RequestParam("code") String code,
                                    @RequestParam("address") String address
    ){
        ShopAddress shopAddress1 = shopService.getShopAddressByMobile(mobile);
        if (shopAddress1 == null){
            return JsonResult.failed("登录失败");
        }
        int result = shopService.update(mobile, address);
        if (result == 0) {
            return JsonResult.failed("成功");
        }
        return JsonResult.success("成功",null);
    }

    // 删除
    @PostMapping("delete")
    public JsonResult delete(@RequestParam("mobile") String mobile,
                             @RequestParam("code") String code
    ){
        ShopAddress shopAddress1 = shopService.getShopAddressByMobile(mobile);
        if (shopAddress1 == null){
            return JsonResult.failed("删除失败");
        }
        int result = shopService.delete(mobile);
        if (result == 0) {
            return JsonResult.failed("删除成功");
        }
        return JsonResult.success("删除成功",null);
    }
}
